package com.company.oa.hr.service.recruitment.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.oa.common.exception.BusinessException;
import com.company.oa.hr.dto.recruitment.ResumeDTO;
import com.company.oa.hr.entity.recruitment.Position;
import com.company.oa.hr.entity.recruitment.Resume;
import com.company.oa.hr.mapper.recruitment.ResumeMapper;
import com.company.oa.hr.service.recruitment.PositionService;
import com.company.oa.hr.service.recruitment.ResumeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume> implements ResumeService {

    @Autowired
    private PositionService positionService;

    @Override
    public IPage<Map<String, Object>> getResumePage(int page, int size, Resume resume) {
        Page<Resume> pageParam = new Page<>(page, size);
        return baseMapper.selectResumePageWithPosition(pageParam, resume);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createResume(ResumeDTO resumeDTO) {
        // 检查职位是否存在
        Position position = positionService.getById(resumeDTO.getPositionId());
        if (position == null) {
            throw new BusinessException("职位不存在");
        }
        
        // 只有已发布状态的职位可以投递简历
        if (position.getStatus() != 1) {
            throw new BusinessException("该职位未在招聘中，不能投递简历");
        }
        
        Resume resume = new Resume();
        BeanUtils.copyProperties(resumeDTO, resume);
        
        // 设置状态为待筛选
        resume.setStatus(1);
        resume.setCreateTime(new Date());
        
        return save(resume);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateResume(ResumeDTO resumeDTO) {
        // 检查简历是否存在
        Resume existResume = getById(resumeDTO.getId());
        if (existResume == null) {
            throw new BusinessException("简历不存在");
        }
        
        Resume resume = new Resume();
        BeanUtils.copyProperties(resumeDTO, resume);
        
        // 设置更新时间
        resume.setUpdateTime(new Date());
        
        return updateById(resume);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteResume(Long id) {
        // 检查简历是否存在
        Resume existResume = getById(id);
        if (existResume == null) {
            throw new BusinessException("简历不存在");
        }
        
        return removeById(id);
    }

    @Override
    public String uploadResumeFile(MultipartFile file) {
        // 实际项目中需要实现文件上传逻辑
        // 这里简单返回一个模拟的文件路径
        return "/uploads/resumes/" + UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateResumeStatus(Long id, Integer status) {
        // 检查简历是否存在
        Resume existResume = getById(id);
        if (existResume == null) {
            throw new BusinessException("简历不存在");
        }
        
        // 检查状态值是否合法
        if (status < 1 || status > 5) {
            throw new BusinessException("简历状态值不合法");
        }
        
        Resume resume = new Resume();
        resume.setId(id);
        resume.setStatus(status);
        resume.setUpdateTime(new Date());
        
        return updateById(resume);
    }
} 